﻿using System;
using System.Collections.Generic;
using Gp.Scripts.Character;
using Gp.Scripts.Core;
using Gp.Scripts.Core.Unit;
using QFramework;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;

namespace Gp.Scripts.Database {
    [Serializable]
    public class CharacterMappingItem {
        [ReadOnly] [TableColumnWidth(40, false)]
        public int id;

        [ReadOnly] [HideLabel] [TableColumnWidth(175, false)]
        public string characterName;
        
        [ReadOnly] [HideLabel] [TableColumnWidth(175, false)]
        public string displayName;
    }


    [Serializable]
    public class ScriptWrapper {
        public string nameSpace;
        public string title;
        [HideInInspector] public string id;
        [HideInInspector] public string value;

        public string description;
    }

    
    public class CharacterModelsDatabase : Singleton<CharacterModelsDatabase>
    {
        [LabelText("默认角色模型")] public BaseCharacter defaultModel;


        [TableList(IsReadOnly = true, ShowPaging = true), ShowInInspector] [SerializeField]
        private List<CharacterMappingItem> characterMappingItems;


        [SerializeField] [ReadOnly] [TableList(IsReadOnly = true, ShowPaging = true), ShowInInspector]
        public List<ScriptWrapper> flowVariableNames;


        public IEnumerable<CharacterMappingItem> CharacterMappingItems => characterMappingItems;


#if UNITY_EDITOR
        [Button("重新加载articy文件角色", ButtonSizes.Large)]
        private void ReLoad() {
            // 清空原有变量
            flowVariableNames.Clear();

            var tableUnit = Global.GetConfig<TableUnit>().DataList;
            foreach (var data in tableUnit) {
                // flowVariableNames.AddRange(database.variables.Where(x => x.Name.Contains("_script"))
                //     .Select(x => new ScriptWrapper()
                //         {
                //             id = x.Name,
                //             title = x.Name.Split('.')[1],
                //             nameSpace = x.Name.Split('_')[0],
                //             value = x.InitialValue,
                //             description = x.Description
                //         }));
                    var item = new CharacterMappingItem {
                        id = data.Id,
                        characterName = data.Name,
                        displayName = data.Name,
                    };
                characterMappingItems.Add(item);
            }
        }


        // [Button("移除不在database中actor")]
        // private void RemoveUnknown() {
        //     if (! EditorUtility.DisplayDialog("确认删除", "此操作不可撤销，是否确认", "确定", "取消")) return;
        //     
        //     var set = new HashSet<string>();
        //
        //     foreach (var database in dialogueDatabase) {
        //         set.AddRange(database.actors.Select(x => x.Name));
        //     }
        //
        //     characterMappingItems.RemoveAll(x => !set.Contains(x.characterName));
        // }
        
        

        private void TryReload() {
            // if (!EditorUtility.DisplayDialog("是否同步更新", "CharacterModelsDatabase: 检测到Dialogue Database变动，是否进行同步更新!",
            //         "确定", "取消")) {
            //     ReLoad();
            // }
        }
#endif
    }
}